# -*- coding: utf-8 -*-
from multiprocessing import Pool
import json,time,sqlite3,MySQLdb,threading
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.proto import rfc1902
worker_processes=5
def getsnmp(ndip,oid):
	errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(cmdgen.CommunityData(ndip, 'public'),cmdgen.UdpTransportTarget((ndip, 161)),oid)
	if errorIndication:
		# print errorIndication
		snmp=None
	else:
		if errorStatus:
			# print '%s at %s\n' % (
				# errorStatus.prettyPrint(),
				# errorIndex and varBinds[int(errorIndex)-1] or '?'
				# )
			snmp=None
		else:
			for name, val in varBinds:
				snmp=val.prettyPrint()
	return snmp
def do_somthing_using(arguments):
	lNode=[]
	s0=s1=None
	ipaddr=arguments[0]
	ip=getsnmp(ipaddr,(1,3,6,1,4,1,5959,1,1,1,1,1,2,1))
	s0=getsnmp(ipaddr,(1,3,6,1,4,1,5959,1,1,11,1,1,3,1,0))
	s1=getsnmp(ipaddr,(1,3,6,1,4,1,5959,1,1,11,1,1,3,1,1))
	lNode.append(ipaddr)
	lNode.append(s0)
	lNode.append(s1)
	lNode.append(ip)
	return lNode
	# print lNode
	# lSNMP.append(lNode)
if __name__ == '__main__':
	# lSNMP=[]

	conn=sqlite3.connect(r'lkt.db3')
	conn.text_factory = str
	nds=conn.execute("select ndip from rnd where bAlive=1").fetchall()
	conn.close()
	print len(nds)
	pool = Pool(processes=worker_processes)              # start 4 worker processes
	start=time.time()
	result=[x.get() for x in [pool.apply_async(do_somthing_using, (x,)) for x in nds]]

	print time.time()-start          # prints "100" unless your computer is *very* slow
	for x in result:
		if not(x[1] and x[2] and x[3]):
			print x
